This post was translated from Korean into English by AI.
Importance Sampling: A method for estimating the expected value of when , where is a random variable that is difficult to sample from, by using a random variable that is easy to sample from.
Proof
Example
For the probability distribution , suppose we want to estimate the expected value of when .
Since is difficult to integrate, it is not easy to sample from it.
- If a function is easy to integrate, it is possible to sample from it using the inverse of its cumulative distribution function and a uniform distribution on [0, 1].
- Of course, in practice, the function above is a normal probability density function with variance , and there are many sampling methods for it, so it can be sampled easily. For the sake of this example, however, let us assume that it is difficult.
Therefore, let us estimate the expected value of by sampling from .
- The theoretical expected value is 0.5.
We can estimate the expected value with the following code.
import numpy as np
def func(x):
return x**2
def p(x):
return np.exp(-(x**2)) / (np.sqrt(np.pi))
def q(x):
# Return proboability density function of a normal distribution
return np.exp(-(x**2) / 2) / (np.sqrt(2 * np.pi))
# Sample from a normal distribution, sample size = 10000
mu = 0
sigma = 1
sample_size = 100000
sample = np.random.normal(mu, sigma, sample_size)
# Calculate the expectation
expectation = np.mean(func(sample) * p(sample) / q(sample))
print(expectation)
The output is 0.5008740539678816, which is very close to 0.5.